package bdhub.littletail.lib.ui;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Iterator;
import java.util.Stack;

import bdhub.littletail.app.LittleTailApplication;

/**
 * Activity管理类
 *
 * Created by Freddy on 2016/2/23.
 * chenshichao@outlook.com
 */
public class CActivityManager {

    private static final CActivityManager INSTANCE = new CActivityManager();

    private static final Stack<Activity> activityStack = new Stack<Activity>();

    private int visibleActivitysCount = 0;

    private CActivityManager() {}

    public static CActivityManager getInstance() {
        return INSTANCE;
    }

    /**
     * 获取可见Activity数
     * @return
     */
    public boolean getAppVisibility() {
        return visibleActivitysCount > 0;
    }

    /**
     * 设置可见Activity数
     * @param visible
     */
    public void setAppVisibility(boolean visible) {
        visibleActivitysCount += visible ? 1 : -1;
    }

    /**
     * 获取栈中元素个数
     * @return
     */
    public int getCount() {
        return activityStack.size();
    }

    /**
     * 添加Activity到栈
     * @param activity
     */
    public void addActivity(Activity activity) {
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（栈顶Activity）
     * @return
     */
    public Activity getTopActivity() {
        if(activityStack.isEmpty()) {
            return null;
        }

        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 获取当前Activity（栈顶Activity）, 没有找到则返回null
     * @param cls
     * @return
     */
    public Activity findActivity(Class<?> cls) {
        Activity activity = null;
        for (Activity act : activityStack) {
            if(act.getClass().equals(cls)) {
                activity = act;
                break;
            }
        }

        return activity;
    }

    /**
     * 结束当前Activity（栈顶Activity）
     */
    public void finishActivity() {
        if(activityStack.size() > 0) {
            Activity activity = activityStack.lastElement();
            finishActivity(activity);
        }
    }

    /**
     * 结束指定的Activity（重载）
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if(null != activity) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定的Activity（重载）
     * @param cls
     */
    public void finishActivity(Class<?> cls) {
        for (Activity act : activityStack) {
            if(act.getClass().equals(cls)) {
                finishActivity(act);
            }
        }
    }

    /**
     * 结束所有的Activity
     * 这个方法非常暴力，在性能好的手机，会把即将启动的 activity 也关掉的
     */
    public void finishAllActivity() {
        Stack<Activity> activities = (Stack<Activity>) activityStack.clone();
        for(Iterator<Activity> iterator = activities.iterator(); iterator.hasNext();) {
            Activity activity = iterator.next();
            if(null != activity) {
                activity.finish();
            }
        }
    }

    /**
     * 结束除指定Activity外的其它Activity
     * @param activity 当前的Activity，即不需要关掉的Activity
     */
    public void finishOtherActivity(Activity activity) {
        for(int i = activityStack.size() - 1; i >= 0; i--) {
            Activity currentActivity = activityStack.get(i);
            if(null != currentActivity && activity != currentActivity) {
                currentActivity.finish();
            }
        }
    }

    /**
     * 结束除指定Activity外的其它Activity
     * @param cls 当前的Activity，即不需要关掉的Activity
     * （重载）
     */
    public void finishOtherActivity(Class<?> cls) {
        for(int i = activityStack.size() - 1; i >= 0; i--) {
            Activity activity = activityStack.get(i);
            if(null != activity && !activity.getClass().equals(cls)) {
                activity.finish();
            }
        }
    }

    /**
     * 重新启动App
     */
    public void restartApp() {
        ActivityManager manager = (ActivityManager) LittleTailApplication.sharedInstance().getSystemService(Context.ACTIVITY_SERVICE);
        manager.restartPackage(LittleTailApplication.sharedInstance().getPackageName());
    }

    /**
     * 应用程序退出
     * @param context
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            Runtime.getRuntime().exit(0);
        }catch (Exception e) {
            Runtime.getRuntime().exit(-1);
        }
    }
}
